package com.ht.right.action;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.ht.right.entity.Button;
import com.ht.right.entity.Menu;
import com.ht.right.entity.MenuToBtn;
import com.ht.right.service.MenuFacadeService;
import com.ht.right.util.CommonUtil;
import com.opensymphony.xwork2.ActionSupport;

public class MenuFacadeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private MenuFacadeService menuFacadeService;
	private Menu menu;
	private Button button;
	private MenuToBtn menuToBtn;
	
	

	//------------------菜单---------------------
	public String  findMenuByHasWF(){
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),menuFacadeService.findMenusHasWF());
		return null;
	}

	public String findMenuById() {
		menu = menuFacadeService.findMenuById(menu.getUuid());
		return "success";
	}
	
	public String findHasRightMenu() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String orgIds = (String) session.getAttribute("orgIds");
		menu = menuFacadeService.findMenuById(menu.getUuid(),orgIds);
		return "success";
	}
	
	
	public String findAllMenu() {
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),menuFacadeService.findAllMenuJson());
		return null;
	}

	public String findSubMenuList() {
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),menuFacadeService.findSubMenuList(menu));
		return null;
	}
	
	public String findMenuTreeNodeList() {
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),menuFacadeService.findMenuTreeNodeList(menu));
		return null;
	}
	
	public String findChildMenuList() {
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),menuFacadeService.findChildMenuList(menu));
		return null;
	}

	public String findRootMenuList() {
		ServletActionContext.getRequest().setAttribute("menuList",menuFacadeService.findRootMenuList());
		return "success";
	}
	
	public String findHasRightMenus() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String orgIds = (String) session.getAttribute("orgIds");
		CommonUtil.toJsonStr(ServletActionContext.getResponse(), menuFacadeService.findMenusData(orgIds));
		return null;
	}

	public String addMenu() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String sameLevelNodeId = request.getParameter("sameLevelNodeId");
		String rigthData = request.getParameter("rightJson");
		String btnIds = request.getParameter("btnData");
		menuFacadeService.addMenu(menu, sameLevelNodeId, rigthData,btnIds);
		String html = null;
		try {
			html = "<script>parent.goCloseDlg(\"oWinDlg\");</script>";
		} catch (Exception e) {
			html = "<script>alert(\"添加失败\");parent.goCloseDlg(\"oWinDlg\");</script>";
			e.printStackTrace();
		}
		CommonUtil.toString(ServletActionContext.getResponse(), html);
		return null;

	}

	public String updateMenu() {
		ServletRequest request = ServletActionContext.getRequest();
		String sameLevelNodeId = request.getParameter("sameLevelNodeId");	//相邻层级节点id
		String position = request.getParameter("position");					//菜单位置(保存当前、指定位置、最后)
		String rigthData = request.getParameter("rightJson");				//菜单查看权限json
		String btnIds = request.getParameter("btnData");					//新增按钮id	
		menuFacadeService.updateMenu(menu, sameLevelNodeId, position, rigthData,btnIds);
		String html = null;
		try {
			html = "<script>parent.goCloseDlg(\"oWinDlg\");</script>";
		} catch (Exception e) {
			html = "<script>alert(\"修改失败\");parent.goCloseDlg(\"oWinDlg\");</script>";
			e.printStackTrace();
		}
		CommonUtil.toString(ServletActionContext.getResponse(), html);
		return null;
	}
	
	//'-----------按钮开始---------------'
	
	public String addButton(){
		ServletRequest request = ServletActionContext.getRequest();
		String rightId = request.getParameter("menuId");
		menuFacadeService.addButton(button,rightId);
		return "success";
	}

	public String findAllButton(){
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),menuFacadeService.findAllButton());
		return null;
	}
	//'-----------按钮结束---------------'
	
	//-------菜单按钮开始------
	public String findMenuBtnById(){
		menuToBtn = menuFacadeService.findMenuBtnById(menuToBtn.getUuid());
		return "success";
	}
	
	public String delMenuBtnById(){
		menuFacadeService.delMenuBtnById(menuToBtn.getUuid());
		return null;
	}
	
	public String updateMenuBtn(){
		ServletRequest request = ServletActionContext.getRequest();
		String rigthData = request.getParameter("rightJson");				//菜单查看权限json
		menuFacadeService.updateMenuBtn(menuToBtn,rigthData);
		String html = null;
		try {
			html = "<script>parent.goCloseDlg(\"oWinDlg\");</script>";
		} catch (Exception e) {
			html = "<script>alert(\"修改失败\");parent.goCloseDlg(\"oWinDlg\");</script>";
			e.printStackTrace();
		}
		CommonUtil.toString(ServletActionContext.getResponse(), html);
		return null;
	}
	//-------菜单按钮结束------
	

	
	public MenuFacadeService getMenuFacadeService() {
		return menuFacadeService;
	}

	public void setMenuFacadeService(MenuFacadeService menuFacadeService) {
		this.menuFacadeService = menuFacadeService;
	}
	
	public Button getButton() {
		return button;
	}

	public void setButton(Button button) {
		this.button = button;
	}
	
	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public MenuToBtn getMenuToBtn() {
		return menuToBtn;
	}

	public void setMenuToBtn(MenuToBtn menuToBtn) {
		this.menuToBtn = menuToBtn;
	}
}
